﻿namespace vKapse.VirtualDesktop.Core
{

	#region Using

	using System;
	using System.Collections.Generic;
	using System.Text;
	using System.Diagnostics;
	using System.Runtime.InteropServices;

	#endregion Using

	/// <summary>
	/// Represents process for object <see cref="Desktop"/>
	/// </summary>
	internal class DesktopProcess
	{

		#region Win32

		private const int SW_HIDE = 0;
		private const int SW_SHOW = 5;
		private const int SW_FORCEMINIMIZE = 11;
		private const int SW_RESTORE = 9;

		[DllImport("User32")]
		private static extern int ShowWindow(int hwnd, int nCmdShow);

		#endregion Win32

		#region Members

		private Desktop m_desktop;
		private string m_module;
		private Process m_process;

		#endregion Members

		#region Constructor

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="process"><see cref="Process"/></param>
		/// <param name="desktop"><see cref="Desktop"/></param>
		public DesktopProcess(Process process, Desktop desktop)
		{
			this.m_desktop = desktop;
			this.m_process = process;
			this.m_module = process.MainModule.ModuleName;
		}

		#endregion Constructor

		#region Properties

		/// <summary>
		/// Gets name of the process
		/// </summary>
		public string Name
		{
			get
			{
				return this.m_module;
			}
		}

		#endregion Properties

		#region Internal Methods

		/// <summary>
		/// Shows this process
		/// </summary>
		internal void Show()
		{
			DesktopProcess.ShowWindow(this.m_process.MainWindowHandle.ToInt32(), DesktopProcess.SW_SHOW);
		}

		/// <summary>
		/// Hides this process
		/// </summary>
		internal void Hide()
		{
			DesktopProcess.ShowWindow(this.m_process.MainWindowHandle.ToInt32(), DesktopProcess.SW_HIDE);
		}

		/// <summary>
		/// Checks if the process is still alive
		/// </summary>
		/// <returns>True if it's alive</returns>
		internal bool CheckAlive()
		{
			return !(this.m_process.HasExited); // Still exists?
		}

		#endregion Internal Methods

	}
}
